package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements Operation {
    @Override
    public void work(BookList list) {
        System.out.println("增加图书...");
        int currentSize = list.getSizeUsed();
        //判满
        if (currentSize == list.getBookList().length) {
            System.out.println("书架已经满了！！！");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名：");
        String bookName = sc.nextLine();
        System.out.println("请输入作者名：");
        String author = sc.nextLine();
        System.out.println("请输入类型：");
        String type = sc.nextLine();
        System.out.println("请输入价格：");
        int price = sc.nextInt();
        for (int i = 0; i < currentSize; i++) {
            //重复
            if (bookName.equals(list.getBook(i).getBookName())) {
                System.out.println("已经有这本书了！！！");
                return;
            }
        }
        //添加
        Book newBook = new Book(bookName,author,type,price);
        list.setBookList(currentSize,newBook);
        //书的数量加1
        list.setSizeUsed(currentSize+1);
        System.out.println("添加成功!!!");
    }
}
